package com.zyz.K_集合.Iterator迭代器;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author: vick
 * @Date: 2025/3/27
 * @Description:
 * @version: 1.0
 */
public class TestIterator {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(111);
        collection.add(222);
        collection.add("vick");
        collection.add("aaa");
        Iterator iterator = collection.iterator();
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        //抛出 NoSuchElementException 异常
        //System.out.println(iterator.next());

        System.out.println("=====");

        Collection collection2 = new ArrayList();
        collection2.add(111);
        collection2.add(222);
        collection2.add("vick");
        collection2.add("aaa");
        Iterator iterator1 = collection2.iterator();
        while (iterator1.hasNext()) { //判断是否还有元素可以迭代
            Object next = iterator1.next();
            System.out.println(next); // 取出下一个元素
            if (next == "vick") {
                iterator1.remove();
            }
        }

        System.out.println(collection2);
    }
}
